<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-31
 * Time: 下午2:44
 */
namespace V5;
class C_Product extends \V4\C_Product {
    public $isActive = true;
    public $remark = '获取产品详情';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!isset($req['id'])) {
            return 'require product id';
        }

        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $req['enterpriseId'], $req['id'] . $req['t'], $req['k']) == false) {
            return 'data illegal';
        }

        if (!isset($req['channelId'])) {
            return 'require channel id';
        }
        $customer_info = \Biz\EnterpriseCustomerHandler::getEnterpriseCustomerById($req['enterpriseId'], $req['uid']);
        $customer_channel_id = $customer_info->getChannelId();
        $customer_enterprise_id = $customer_info->getEnterpriseId();

        if ($customer_channel_id != $req['channelId']) {    //验证用户渠道
            return '渠道商品获取失败!';
        }
        if ($customer_enterprise_id != $req['enterpriseId']) {//验证用户企业
            return '企业用户获取失败!';
        }
        $product_handler = new \Biz\ProductHandler();
        $product_cls = $product_handler->getProductById($req['id']);
        /**
         * @var $product_cls \Cls\Product\Product
         */
        $product_cls_temp = new \stdClass();
        $product_cls_temp->id = $product_cls->getId();
        $product_cls_temp->enterprise_id = $product_cls->getEnterpriseId();
        $product_cls_temp->brand_id = $product_cls->getBrandId();
        $product_cls_temp->name = $product_cls->getName();
        $product_cls_temp->icon_url = $product_cls->dealIconUrl();
        $product_cls_temp->repertory_warning = $product_cls->getStockWarning();
        $product_cls_temp->individual_packing = $product_cls->getSellUnitString(); // 老版本
        $product_cls_temp->mass_packing = $product_cls->getSellUnitString(); // 老版本
        $product_cls_temp->sell_unit_string = $product_cls->getSellUnitString();
        $product_cls_temp->remark = $product_cls->getDescription();
        $product_cls_temp->insert_time = $product_cls->getCreateTime();
        $product_cls_temp->is_del = $product_cls->getDelStatus();
        /**
         * 增加富文本信息
         */
        if ($_SERVER['HTTP_HOST'] == 'rest.test.freshqiao.com') {
            $product_cls_temp->rich_text_url = 'http://enterprise.test.freshqiao.com/product/detail-description-rich?id='.$product_cls_temp->id;
        } else if ($_SERVER['HTTP_HOST'] == 'rest.freshqiao.com') {
            $product_cls_temp->rich_text_url = 'http://enterprise.freshqiao.com/product/detail-description-rich?id='.$product_cls_temp->id;
        } else {
            $product_cls_temp->rich_text_url = '';
        }
        $brand_cls = $product_cls->getBrandInfo();
        if ($brand_cls instanceof \Cls\Brand\Brand) {
            $brand_name = $brand_cls->getName();
        } else {
            $brand_name = '未知分类名称';
        }
        $product_cls_temp->brandname = $brand_name;
        $channel_list = $product_cls->getChannelProduct();
        if (empty($channel_list) || !isset($channel_list[$customer_channel_id])) {
            $result = array();
            return '您所在渠道没有此商品，或商品已下架';
        }
        $product_cls_temp->channel_price = $channel_list[$customer_channel_id]->getPriceRange();
        $product_cls_temp->collect_stock = $product_cls->getRepertoryTotal();
        $product_cls_temp->stock_warning = $product_cls->judgeStockWaring();
        $product_cls_temp->scroller_imgs = array();
        $product_cls_temp->detail_imgs [] = $product_cls_temp->rich_text_url;
        $image_list = $product_cls->getAllImageList();
        foreach ($image_list as $image_list => $image_cls) {
            if ($image_cls->getType() == \Cls\Product\Image::IMAGE_TYPE_PRODUCT) {
                $product_cls_temp->scroller_imgs [] = 'http://image.freshqiao.com/' . $image_cls->getUrl();
            }
        }

        $result = $product_cls_temp;
        if ($result) {
            $collection_handler = new \Biz\CustomerCollectionHandler();
            $collection_status = $collection_handler::getCollectionState($customer_enterprise_id, $req['id'], $customer_channel_id);
            $collection_status = $collection_status ? $collection_status->getIsDel() : '0';
            $result->collection_status = (string)$collection_status;

            $result->activity_info = array();
            $product_channel_handler = new \Biz\ProductHandler();
            $product_sku_list = $product_channel_handler->getOnsaleAttrByProductIdsAndChannelId(array($product_cls->getId()),$customer_channel_id);// 得到sku信息
            \Biz\Market\Product::handelSkuListPriceByChannelIdAndEnterpriseId($product_sku_list,$customer_channel_id,$customer_enterprise_id); // 根据渠道id批量处理sku价格，与参与活动状态
            if (isset($product_sku_list[$product_cls->getId()]) == false ) {
                return '您所在渠道没有此商品，或商品已下架';
            }
            $result->sku_info = $product_sku_list[$product_cls->getId()];
            if(isset($result->sku_info[0]['join_market_type'])) {
                $market_cls_temp = new \stdClass();
                $market_cls_temp->id = $result->sku_info[0]['join_market_type']->getId();
                $market_cls_temp->title = $result->sku_info[0]['join_market_type']->getTitle();
                $market_cls_temp->type = $result->sku_info[0]['join_market_type']->getType();
                $market_cls_temp->condition = $result->sku_info[0]['join_market_type']->getCondition();
                $market_cls_temp->discount = $result->sku_info[0]['join_market_type']->getDicount();
                $market_cls_temp->rewards = $result->sku_info[0]['join_market_type']->getRewards();
                $market_cls_temp->product_type = $result->sku_info[0]['join_market_type']->getProductType();
                $market_cls_temp->channel_type = $result->sku_info[0]['join_market_type']->getChannelType();
                $market_cls_temp->start_time = $result->sku_info[0]['join_market_type']->getStartTime();
                $market_cls_temp->end_time = $result->sku_info[0]['join_market_type']->getEndTime();
                $market_cls_temp->status = $result->sku_info[0]['join_market_type']->getStatus();
                $market_cls_temp->enterprise_id = $result->sku_info[0]['join_market_type']->getEnterpriseId();
                $market_cls_temp->unit = $result->sku_info[0]['join_market_type']->getUnit();
                $market_cls_temp->create_time = $result->sku_info[0]['join_market_type']->getCreateAt();
                $market_cls_temp->edit_time = $result->sku_info[0]['join_market_type']->getUpdateAt();
                $market_cls_temp->uniform_price = $result->sku_info[0]['join_market_type']->getUniformPrice();
                $market_cls_temp->update_time = $result->sku_info[0]['join_market_type']->getUpdateAt();
                $market_cls_temp->is_del = (string)$result->sku_info[0]['join_market_type']->getIsDel();
                array_push($result->activity_info, $market_cls_temp);
            }
        } else {
            $result = array();
            return '您所在渠道没有此商品，或商品已下架';
        }
    }
}